package com.spa.infrastructure.repository.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.spa.infrastructure.repository.mapper.SysUserMapper;
import com.spa.infrastructure.util.StringUtils;
import com.spa.infrastructure.repository.condition.SysUserCondition;
import com.spa.infrastructure.repository.po.SysUserPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class SysUserRepositoryService extends MPJBaseServiceImpl<SysUserMapper, SysUserPO> {

    public Page<SysUserPO> page(SysUserCondition condition) {
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper(condition));
    }

    public List<SysUserPO> list(SysUserCondition condition) {
        return list(wrapper(condition));
    }

    private LambdaQueryWrapper<SysUserPO> wrapper(SysUserCondition condition) {
        LambdaQueryWrapper<SysUserPO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotNull(condition.getUsername()),SysUserPO::getUserName,condition.getUsername());
        wrapper.orderByDesc(SysUserPO::getCreateTime);
        return wrapper;
    }

}
